var danmu = function (vue) {
  //1.获取元素
  var oBox = document.querySelector('.box')   //获取.box元素
  //var oCon = document.querySelector('.con')   //获取input框
  var cW = oBox.offsetWidth   //获取box的宽度
  var cH = oBox.offsetHeight   //获取box的高度
  vue.$$createEle = function createEle (txt) {

    //动态生成span标签
    var oMessage = document.createElement('span')   //创建标签
    oMessage.innerHTML = txt   //接收参数txt并且生成替换内容
    oMessage.style.left = cW + 'px'  //初始化生成位置x
    oBox.appendChild(oMessage)   //把标签塞到oBox里面
    vue.$$roll.call(oMessage, {
      //call改变函数内部this的指向
      timing: ['linear', 'ease-out'][~~(Math.random() * 2)],
      color: '#' + (~~(Math.random() * (1 << 24))).toString(16),
      top: vue.$$random(0, cH),
      fontSize: vue.$$random(16, 32)
    })
  }

  vue.$$roll = function roll (opt) {
    //弹幕滚动
    //如果对象中不存在timing 初始化
    opt.timing = opt.timing || 'linear'
    opt.color = opt.color || '#fff'
    opt.top = opt.top < 130 ? 130 :  opt.top -130
    opt.fontSize = opt.fontSize || 16
    this._left = parseInt(this.offsetLeft) -200   //获取当前left的值
    this.style.color = opt.color   //初始化颜色
    this.style.top = opt.top + 'px'
    this.style.fontSize = opt.fontSize + 'px'
    this.timer = setInterval(function () {
      if (this._left <= 100) {
        clearInterval(this.timer)   //终止定时器
        this.parentNode.removeChild(this)
        return   //终止函数
      }
      switch (opt.timing) {
        case 'linear':   //如果匀速
          this._left += -2
          break
        case 'ease-out':   //
          this._left += (0 - this._left) * .01
          break
      }
      this.style.left = this._left + 'px'
    }.bind(this), 1000 / 60)
  }

  vue.$$random = function random (start, end) {
    //随机数封装
    return start + ~~(Math.random() * (end - start))
  }
//产生闭包
  var obj = {
    num: 1,
    add: function () {
      this.num++;   //obj.num = 2;
      (function () {
        console.log(this.num)
      })
    }
  }
  obj.add()  //window

}
export {
  danmu
}
